# coding: UTF-8
'''
Created on 2011-5-6

@author: yang_wanjun
'''
import datetime
from django import forms
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import gettext_lazy as _
from django.utils.encoding import force_unicode
from senbzblog.utils import constant
from senbzblog.comments.models import Comment

class CommentForm(forms.Form):
    # Basic
    user_id = forms.CharField(max_length=30, required=False, widget=forms.HiddenInput)
    user_name = forms.CharField(label=_("Name"), max_length=50)
    user_email = forms.EmailField(label=_("Email address"))
    user_url = forms.URLField(label=_("URL"), required=False)
    comment = forms.CharField(label=_('Comment'), widget=forms.Textarea,
                                    max_length=constant.COMMENT_MAX_LENGTH)
    # Content-object field
    content_type = forms.CharField(widget=forms.HiddenInput)
    object_pk = forms.CharField(widget=forms.HiddenInput)
    
    parent_id = forms.IntegerField(widget=forms.HiddenInput, initial=0, required=False)
    is_mailnotify = forms.BooleanField(initial=False, required=False)
    pertain = forms.IntegerField(widget=forms.HiddenInput, initial=None, required=False)
    
    def __init__(self, target_object, data=None, initial=None, pertain=None, user_id=None):
        self.target_object = target_object
        self.pertain = pertain
        self.user_id = user_id
        if initial is None:
            initial = {}
        initial.update({
            'content_type'  : str(self.target_object._meta),
            'object_pk': str(self.target_object._get_pk_val()),
        })
        super(CommentForm, self).__init__(data=data, initial=initial, auto_id=True)
    
    def get_comment_object(self):
        if not self.is_valid():
            raise ValueError("get_comment_object may only be called on valid forms")
        
        ret = Comment(
            user_id=self.cleaned_data['user_id'],
            user_name=self.cleaned_data['user_name'],
            user_email=self.cleaned_data['user_email'],
            user_url=self.cleaned_data['user_url'],
            comment=self.cleaned_data['comment'],
            submit_date=datetime.datetime.now(),
            
            content_type=ContentType.objects.get_for_model(self.target_object),
            object_pk=force_unicode(self.target_object._get_pk_val()),
            site_id=settings.SITE_ID,
            
            is_mailnotify=self.cleaned_data['is_mailnotify'],
            parent_id=self.cleaned_data['parent_id'],
            pertain=self.pertain
        )
        if ret.parent_id == 0:
            ret.parent_id = None
        if ret.user_url:
            if not ret.user_url.startswith("http://") and not ret.user_url.startswith("https://"):
                ret.user_url = "http://" + ret.user_url
            
        return ret

class ArticleCommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('user_name', 'user_email', 'user_url', 'comment', 'is_public')
        widgets = {'comment':forms.Textarea(attrs={'style':'width:100%;'}), }
